Python Deep Learning (Python Technologies) by Vishal Gupta
Author: Vishal Gupta [Gupta, Vishal] , Date: November 8, 2020
, Views: 945
Removing an entire table using Python
You can drop a table whenever you need to, using the DROP statement. But you need to be very careful while deleting any existing table because the data lost will not be recovered after deleting a table.
import psycopg2
#establishing the connection
conn = psycopg2. connect(
database = "mydb" , user = 'postgres' , password = 'password' , host = '127.0.0.1' , port = '5432'
)
#Setting auto commit false
conn. autocommit = True
#Creating a cursor object using the cursor() method
cursor = conn. cursor()
#Doping EMPLOYEE table if already exists
cursor. execute( "DROP TABLE emp" )
print ( "Table dropped... " )
#Commit your changes in the database
conn. commit()
#Closing the connection
conn. close()
Copyright Disclaimer:
This site does not store any files on its server. We only index and link
to content provided by other sites. Please contact the content providers
to delete copyright contents if any and email us, we'll remove relevant
links or contents immediately.